import Vue from 'vue';
import VueRouter from 'vue-router';
import Layout from '../layout/Layout.vue';
import AppMain from '../layout/AppMain'
import Home from "../page/Example.vue";
// 页面跳转处理多语言
import {getLangLocale} from "../utils/i18n";
import i18n from "../lang";
//首页管理
import floorInfo from "../page/content/homeManagement/floor_info";
import basicInformation from "../page/content/homeManagement/basic_information";
import advertisingManagement from "../page/content/homeManagement/advertising_management";
import articleManagement from "../page/content/homeManagement/article_management";
//系统管理
import paramSetting from "../page/sysSetting/paramSetting";

Vue.use(VueRouter);


const router = new VueRouter({
    //mode: 'history',
    scrollBehavior: () => ({y: 0}),
    routes: [
        // {
        //     name: '',
        //     path: '/',
        //     redirect: '/home',
        //     hidden: true
        // },

        {
            path: '/redirect',
            component: Layout,
            hidden: true,
            children: [
                {
                    path: '/redirect/:path*',
                    component: () => import('../page/redirect/index')
                },
            ]
        },

        {
            name: '',
            path: '/',
            component: Layout,
            redirect: '/home',
            children: [
                {
                    path: '/home',
                    component: AppMain, //二级目录组件为AppMain
                    redirect: '/home', //该目录下第一个页面的路由
                    children: [
                        {
                            name: 'home',
                            path: '/home',
                            component: Home
                        },
                    ]
                }
            ]
        },

        {
            name: 'contentManagement',
            path: '/content',
            component: Layout,
            redirect: 'noredirect',
            children: [
                {
                    path: '/homeManagement',
                    component: AppMain, //二级目录组件为AppMain
                    redirect: '/content/homeManagement', //该目录下第一个页面的路由
                    children: [
                        {
                            name: 'floorInfo',
                            path: '/content/homeManagement/floorInfo',
                            component: floorInfo
                        }, {
                            name: 'basicInformation',
                            path: '/content/homeManagement/basicInformation',
                            component: basicInformation
                        }, {
                            name: 'advertisingManagement',
                            path: '/content/homeManagement/advertisingManagement',
                            component: advertisingManagement
                        }, {
                            name: 'articleManagement',
                            path: '/content/homeManagement/articleManagement',
                            component: articleManagement
                        }
                    ]
                },
                {
                    path: '/sysSetting',
                    component: AppMain, //二级目录组件为AppMain
                    redirect: '/sysSetting', //该目录下第一个页面的路由
                    children: [
                        {
                            name: 'paramSetting',
                            path: '/sysSetting/paramSetting',
                            component: paramSetting
                        }
                    ]
                }
            ]
        },

    ]
});

export default router;

// 全局导航钩子
router.beforeEach((to, from, next) => {
    // console.log(from);
    // console.log(to.name);

    //获取版本号，判断是否更新缓存
    let nowVersion = localStorage.getItem("bbc_version");
    let oldVersion = localStorage.getItem("old_bbc_version");
    if (nowVersion && nowVersion !== oldVersion) {
        //清理缓存
        Vue.prototype.utils.clearStorage();
        localStorage.setItem("old_bbc_version", nowVersion)
    }

    //请求多语言
    let language = i18n.locale;
    // console.log("router -> language: " + i18n.locale)
    //请求common多语言数据
    getLangLocale(language, "common", "", () => {
        //请求page的多语言数据
        getLangLocale(language, "pages", to.name, () => {
            //获取到多语言后 再进入路由，避免弹窗中和校验提示语未翻译
            next();
        });
    });

    // menu不存在，则直接进入路由
    if (!sessionStorage.getItem("sd_menus")) {
        next();
    }
})
